--
-- Author: shunguo.chen
-- Date: 2018-07-10 17:00:44
--
-- ItemGetActView	道具获得界面，有动画表现
--

local ItemGetActRender = import(".ItemGetActRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.ItemGetActScene

local MAX_MOVE_NUM = 3

local START_ANI_NAME = "appear"
local MID_ANI_NAME = "flicker"
local END_ANI_NAME = "disappear"

local RENDER_ACTION_TAG = 1

local MOVE_SCALES = {1, 0.9, 0.81}

function ClassRef:init()
	-- bag ani node
	self._ui.bg:setVisible(false)
	self._ui.bg:setOpacity(0)

	local bagAniNode = self._ui.ani_get

	local timeline = bagAniNode:getTimeline()
	timeline:gotoFrameAndPause(0)
	timeline:setLastFrameCallFunc(function()
		self:_playBagAniEnd()
	end)
	self._bagTimeline = timeline

	-- fly icon
	self._flyIconBg = self._ui.icon_bg
	self._flyIcon = self._flyIconBg:getChildByName("icon")
	self._flyStartPosX, self._flyStartPosY = self._flyIconBg:getPosition()
	
	local posX, posY = bagAniNode:getPosition()
	local width, height = bagAniNode:getChildByName("Panel_1"):getContentSize2()
	self._flyEndPosX = posX + width/2
	self._flyEndPosY = posY + height/2

	-- move renders
	local renders = {0,0,0}
	local renderPosYAry = {0,0,0}
	for i=1,3 do
		local render = ItemGetActRender.create(self._ui["huode_" .. i])
		renders[i] = render
		renderPosYAry[i] = render:getPositionY()
		render:setVisible(false)
	end

	self._renders = renders
	self._renderPosYAry = renderPosYAry
	self._datas = {}
	self._moveRenders = {}
end

function ClassRef:clearItems()
	for i,render in ipairs(self._moveRenders) do
		render:stopAllActionsByTag(RENDER_ACTION_TAG)
		render:stopTimeline()
		render:setVisible(false)
		self._renders[#self._renders + 1] = render
	end
	
	self._moveRenders = {}
	self._datas = {}

	self._flyIconBg:stopAllActions()
	self._flyIconBg:setVisible(false)

	self._bagTimeline:gotoFrameAndPause(0)
	self._ui.bg:stopAllActions()
	self._ui.bg:setVisible(false)
	self._ui.bg:setOpacity(0)
end

-- data = {item=Item/Equip/Jewel/MountSoul/Daibi, num=5}
function ClassRef:addItem(data)
	local moveNum = #self._moveRenders
	if moveNum < MAX_MOVE_NUM then
		local idx = moveNum + 1
		local render = table.remove(self._renders, 1)
		render:setData(data)
		render:setPositionY(self._renderPosYAry[idx])
		render:setVisible(true)
		render:setOpacity(0)
		render:setScale(MOVE_SCALES[idx])
		self._moveRenders[idx] = render

		local actions = {cca.fadeIn(0.3)}
		if idx == 1 then
			local delay = cca.delay(0.6)
			local playCB = cca.cb(function() render:playTimeline() end)
			local endCB = cca.cb(handler(self, self._actionOver))
			actions[#actions + 1] = cca.spawn({delay, playCB})
			actions[#actions + 1] = endCB
		end
		local action = cca.seq(actions)
		action:setTag(RENDER_ACTION_TAG)
		render:runAction(action)

		if moveNum == 0 then
			self:_playBagAni(START_ANI_NAME)
		end
	else
		self._datas[#self._datas + 1] = data
	end
end

function ClassRef:_actionOver()
	-- remove first render
	local render = table.remove(self._moveRenders, 1)
	render:setVisible(false)
	self._renders[#self._renders + 1] = render

	-- icon fly
	local data = render:getData()
	local item = data.item
	self._flyIconBg:setVisible(true)
	self._flyIconBg:setScale(1)
	self._flyIconBg:setPosition(self._flyStartPosX, self._flyStartPosY)
	self._flyIconBg:loadTexture(item:getIconBg())
	self._flyIcon:loadTexture(item:getIcon())

	local move = cca.moveTo(0.3, self._flyEndPosX, self._flyEndPosY)
	local scale = cca.scaleTo(0.3, 0.01)
	local hide = cca.hide()
	local endCB = cca.cb(function() self:_playBagAni(MID_ANI_NAME) end)
	local action = cca.seq({cca.spawn({move, scale}), hide, endCB})
	self._flyIconBg:runAction(action)

	-- move other render
	if #self._moveRenders > 0 then
		for i,render in ipairs(self._moveRenders) do
			local move = cca.moveBy(0.3, 0, self._renderPosYAry[i] - self._renderPosYAry[i+1])
			local scale = cca.scaleTo(0.3, MOVE_SCALES[i])
			local action = cca.spawn({move, scale})
			if i == 1 then
				local delay = cca.delay(0.3)
				local playCB = cca.cb(function() render:playTimeline() end)
				local endCB = cca.cb(function() self:_actionOver() end)
				action = cca.seq({action, cca.spawn({delay, playCB}), endCB})
			end
			action:setTag(RENDER_ACTION_TAG)
			render:runAction(action)
		end

		if #self._datas > 0 then
			self:addItem(table.remove(self._datas, 1))
		end
	end
end

function ClassRef:_playBagAniEnd()
	if self._curAniName == MID_ANI_NAME then
		if #self._moveRenders == 0 then
			-- 没有移动的render，播放结束动画
			self:_playBagAni(END_ANI_NAME)
		end
	elseif self._curAniName == END_ANI_NAME then
		self._ui.bg:runAction(cca.seq({cca.fadeOut(0.2), cca.hide()}))
	end
end

function ClassRef:_playBagAni(name)
	if name == START_ANI_NAME then
		self._ui.bg:runAction(cca.seq({cca.show(), cca.fadeIn(0.2)}))
	end
	self._curAniName = name
	self._bagTimeline:play(name, false)
end

return ClassRef
